package com.east.config.handler;

import cn.hutool.json.JSONUtil;
import com.east.result.Result;
import com.east.utils.ResponseUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 登陆失败处理器
 */
@Component
public class CustomAuthenticationFailureHandler implements AuthenticationFailureHandler {
    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
        System.out.println("登陆失败..." + exception.getMessage());
        Result fail = Result.FAIL(exception.getMessage());
        String jsonStr = JSONUtil.toJsonStr(fail);
        System.out.println("登陆失败");
        ResponseUtils.write(response, jsonStr);
    }
}
